<?php
//
// 购物处理控制器
//
class Controller_App_Buy extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/buy/buy_now";
        parent::__construct($req, $res);
    }

    //
    // 直接购买
    //
    public function action_now ()
    {
        $goodsId = $this->request->query('goods');
        $attrId  = $this->request->query('attr');
        $counts  = $this->request->query('count');

        $query = array();
        $key = "quan_".$goodsId."_".$attrId;
        $query[$key] = $counts;

        $this->buy_now($query);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '支付', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    //
    // 拼单购买
    //
    public function action_tuan ()
    {
        $goodsId = $this->request->query('goods');
        $attrId  = $this->request->query('attr');
        $counts  = $this->request->query('count');
        $tuanId  = $this->request->query('tuanId');

        $query = array();
        $key = "quan_".$goodsId."_".$attrId;
        $query[$key] = $counts;

        $this->buy_tuan($query, $tuanId);
    }

 

    //
    // 代付入口
    //
    public function action_paidByOther ()
    {
        $goodsId = $this->request->query('goods');
        $attrId  = $this->request->query('attr');
        $counts  = $this->request->query('count');

        $query = array();
        $key = "quan_".$goodsId."_".$attrId;
        $query[$key] = $counts;

        $this->paid_by_other($query);
    }

    //
    // 我要送人
    //
    public function action_gift ()
    {
        $goodsId = $this->request->query('goods');
        $attrId  = $this->request->query('attr');
        $counts  = $this->request->query('count');

        $query = array();
        $key = "quan_".$goodsId."_".$attrId;
        $query[$key] = $counts;

        $this->giving_gift($query);
    }




    //
    // 购物车生成订单
    //
    public function action_cart ()
    {
        $query = $this->request->query();

        // 删除购物车
        $cartIds = Arr::get($query, 'cartid', array());
        App_Cart::remove($this->_user, $cartIds);

        $this->buy_now($query);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '支付', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

	/**
     * 显示订单信息
     * @param $query 商品信息 将订单信息取出并显示
     */
    protected function buy_now ($query)
    {
        $arrBuyGoods = array();
        $arrBuyShops = array();
        $goodsQuantityTotal = 0;
        foreach ($query as $key => $val)
        {
            if (preg_match('/^quan_/', $key))
            {
                list($prefix, $goodsId, $goodsAttrId) = explode('_', $key);
                $quantity = (int) $val;
                $goodsQuantityTotal += $quantity;
                $arrBuyGoods[] = App_Goods::basicInfo($goodsId, $goodsAttrId, $quantity);
            }
        }

        // 按店铺分组的商品信息
        $arrBuyShops = App_Goods::goodsInfoGroupByShop ($arrBuyGoods);

        // 地址信息
        $addressId = $this->request->query('addressId');
        $address   = App_Address::defaultAddr($this->_user, $addressId);

        // 支付信息
        $payment   = App_Payment::infoList ();

        // 用户账户
        $account   = App_Account::account($this->_user);

        // 发票配置(系统配置)
        $invoice   = App_Invoice::optionList ();

        // 快递
        $deliver   = App_Deliver::infoList ($arrBuyGoods, $address);

        // 金额信息
        $money     = App_Payment_Money::get($arrBuyGoods, $address, $account, $deliver);

        $this->template->set_filename('smarty:app/buy/buy_now');
        $this->template->set('buyType', 'normal');
        $this->template->set('goodsList', $arrBuyGoods);
        $this->template->set('goodsShopList', $arrBuyShops);
        $this->template->set('goodsQuantityTotal', $goodsQuantityTotal);
        $this->template->set('address', $address);
        $this->template->set('payment', $payment);
        $this->template->set('account', $account);
        $this->template->set('money', $money);
        $this->template->set('invoice', $invoice);
        $this->template->set('deliver', $deliver);
    }

	/**
     * 显示团购订单生成信息
     * @param $query 商品信息 将订单信息取出并显示
     */
    protected function buy_tuan ($query, $tuanId)
    {
        $arrBuyGoods = array();
        $arrBuyShops = array();
        $goodsQuantityTotal = 0;
        foreach ($query as $key => $val)
        {
            if (preg_match('/^quan_/', $key))
            {
                list($prefix, $goodsId, $goodsAttrId) = explode('_', $key);
                $quantity = (int) $val;
                $goodsQuantityTotal += $quantity;
                $arrBuyGoods[] = App_Goods::basicInfo($goodsId, $goodsAttrId, $quantity, 'tuan');
            }
        }

        // 按店铺分组的商品信息
        $arrBuyShops = App_Goods::goodsInfoGroupByShop ($arrBuyGoods);

        // 地址信息
        $addressId = $this->request->query('addressId');
        $address   = App_Address::defaultAddr($this->_user, $addressId);

        // 支付信息
        $payment   = App_Payment::infoList ();

        // 用户账户
        $account   = App_Account::account($this->_user);

        // 发票配置(系统配置)
        $invoice   = App_Invoice::optionList ();

        // 快递
        $deliver   = App_Deliver::infoList ($arrBuyGoods, $address);

        // 金额信息
        $money     = App_Payment_Money::get($arrBuyGoods, $address, $account, $deliver);

        $this->template->set_filename('smarty:app/buy/buy_tuan');
        $this->template->set('buyType', 'tuan');
        $this->template->set('tuanId', $tuanId);
        $this->template->set('goodsList', $arrBuyGoods);
        $this->template->set('goodsShopList', $arrBuyShops);
        $this->template->set('goodsQuantityTotal', $goodsQuantityTotal);
        $this->template->set('address', $address);
        $this->template->set('payment', $payment);
        $this->template->set('account', $account);
        $this->template->set('money', $money);
        $this->template->set('invoice', $invoice);
        $this->template->set('deliver', $deliver);
    }


	/**
     * 代付信息补充界面
     * @param $query 商品信息 将订单信息取出并显示
     */
    protected function paid_by_other ($query)
    {
        $arrBuyGoods = array();
        foreach ($query as $key => $val)
        {
            if (preg_match('/^quan_/', $key))
            {
                list($prefix, $goodsId, $goodsAttrId) = explode('_', $key);
                $quantity = (int) $val;
                $arrBuyGoods[] = App_Goods::basicInfo($goodsId, $goodsAttrId, $quantity);
            }
        }

        // 地址信息
        $address   = App_Address::infoList($this->_user);

        // 发票配置(系统配置)
        $invoice   = App_Invoice::optionList ();

        // 快递
        $deliver   = App_Deliver::infoList ($arrBuyGoods, $address);

        // 金额信息
        $account   = array('coin' => 0);    
        $money     = App_Payment_Money::get($arrBuyGoods, $address, $account, $deliver);

        $this->template->set_filename('smarty:app/buy/paid_by_other');
        $this->template->set('goodsList', $arrBuyGoods);
        $this->template->set('address', $address);
        $this->template->set('money', $money);
        $this->template->set('invoice', $invoice);
        $this->template->set('deliver', $deliver);
    }

	/**
     * 显示订单信息
     * @param $query 商品信息 将订单信息取出并显示
     */
    protected function giving_gift ($query)
    {
        $arrBuyGoods = array();
        foreach ($query as $key => $val)
        {
            if (preg_match('/^quan_/', $key))
            {
                list($prefix, $goodsId, $goodsAttrId) = explode('_', $key);
                $quantity = (int) $val;
                $arrBuyGoods[] = App_Goods::basicInfo($goodsId, $goodsAttrId, $quantity);
            }
        }

        // 支付信息
        $payment   = App_Payment::infoList ();

        // 快递
        $address   = array();
        $deliver   = App_Deliver::infoList ($arrBuyGoods, $address);

        // 金额信息
        $account   = array('coin' => 0);    
        $money     = App_Payment_Money::get($arrBuyGoods, $address, $account, $deliver);

        $this->template->set_filename('smarty:app/buy/gift');
        $this->template->set('goodsList', $arrBuyGoods);
        $this->template->set('payment', $payment);
        $this->template->set('deliver', $deliver);
        $this->template->set('money', $money);
        $this->template->set('title', '我要送人：礼物付款');
    }

	public function before ()
	{
		parent::before();
		if ($this->_uid == 0)
		{
			$this->request->redirect('/login');
		}
	}
}
